﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
using CsvHelper.Configuration;

namespace EmotionsExtract.Emotion
{
    internal class EmotionParser
    {
        private static readonly Encoding gb2312 = Encoding.GetEncoding(936);

        private static Dictionary<int, string> ParseGroupMaster(string groupDef)
        {
            var groupMaster = new Dictionary<int, string>();
            using (var reader = new StreamReader(groupDef, gb2312))
            using (var tsv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture) { 
                Delimiter = "\t",
                BadDataFound = null,
            }))
            {
                var scheme = new
                {
                    pageid = default(int),
                    name = default(string),
                };
                foreach (var iconItem in tsv.GetRecords(scheme))
                    groupMaster.Add(iconItem.pageid, iconItem.name);
            }
            return groupMaster;
        }

        public static EmotionGroup[] Parse(string groupDef, string itemDef)
        {
            var groupIconList = new Dictionary<int, List<EmotionItem>>();
            using (var reader = new StreamReader(itemDef, gb2312))
            using (var tsv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture) { 
                Delimiter = "\t",
                BadDataFound = null,
            }))
            {
                var scheme = new
                {
                    id = default(int),
                    command = default(string),
                    Type = default(string),
                    imgfile = default(string),
                    Frame = default(int),
                    pageid = default(int?),
                };
                var lastImageFile = string.Empty;
                var lastPageId = 0;
                foreach (var iconItem in tsv.GetRecords(scheme))
                {
                    if(!string.IsNullOrEmpty(iconItem.imgfile))
                        lastImageFile = Path.GetFileNameWithoutExtension(iconItem.imgfile);
                    if (iconItem.pageid != null)
                        lastPageId = iconItem.pageid.Value;
                    else if (iconItem.command.StartsWith("#p"))     // WARN: Workaround
                        lastPageId = 17;
                    if(!string.IsNullOrEmpty(iconItem.command))
                    {
                        var emotion = new EmotionItem(iconItem.id, iconItem.command, iconItem.Type == "animate", lastImageFile, iconItem.Frame);
                        if (!groupIconList.ContainsKey(lastPageId))
                            groupIconList.Add(lastPageId, new List<EmotionItem>());
                        groupIconList[lastPageId].Add(emotion);
                    }
                }
            }
            var groupNames = ParseGroupMaster(groupDef);
            var outputGroups = new List<EmotionGroup>();
            foreach (var group in groupIconList)
            {
                var groupName = groupNames[group.Key];
                outputGroups.Add(new EmotionGroup(group.Key, groupName, group.Value.ToArray()));
            }
            return outputGroups.ToArray();
        }
    }
}
